package fun.tan90.easy.binlog.manager.handler;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.mybatisflex.core.row.Db;
import fun.tan90.easy.binlog.base.constants.EasyBinlogConstants;
import fun.tan90.easy.binlog.base.enums.OutputResEnum;
import fun.tan90.easy.binlog.base.model.OutputCount;
import fun.tan90.easy.binlog.base.strategy.AbstractExecuteStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * description TODO
 * company 铁人科技
 *
 * @author 陈浩杰
 * @date 2023/12/15 19:42
 */
@Slf4j
@Component
public class OutputCountHandler implements AbstractExecuteStrategy<String, Void> {

    @Override
    public String mark() {
        return EasyBinlogConstants.OUTPUT_COUNT;
    }

    @Override
    public void execute(String outputCountJson) {
        OutputCount outputCount = JSONUtil.toBean(outputCountJson, OutputCount.class);
        OutputResEnum outputRes = outputCount.getOutputRes();
        String outputCountId = outputCount.getOutputCountId();
        String updateSql = StrUtil.format("UPDATE binlog_output_count set {} WHERE id = ?", OutputResEnum.SUCCESS.equals(outputRes) ? "success = success + 1, count_success_time = ?" : "error = error + 1, count_error_time = ?");
        Db.updateBySql(updateSql, new Date(), outputCountId);
    }
}
